/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.StafferBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.DepartmentDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.SerialNumberDao;
import com.minierp.dao.StafferDao;
import com.minierp.dao.UserDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.CodeRule;
import com.minierp.model.Staffer;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class StafferFormCtr extends SimpleFormController {
	private DepartmentDao departmentDao;
	private UserDao userDao;
	private StafferDao stafferDao;
	private CodeRuleDao codeRuleDao;
	private SerialNumberDao serialNumberDao;
	private IdGenerator idGenerator;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		String departmentId = request.getParameter("department_id");
		refData.put("departmentId", departmentId);
		refData.put("departmentList", this.departmentDao.getList(true));
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		CodeRule cr = this.codeRuleDao.get("STAFFER");
		if (cr == null) {
			throw new Exception("Code rule not found for staffer");
		} else {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			StafferBean form = new StafferBean();
			form.setCodeRule(cr);
			Staffer staffer;
			if (id != null && !id.equals("")) {
				staffer = (Staffer) this.stafferDao.get(Staffer.class,
						Long.parseLong(id));
				form.setStaffer(staffer);
				if (staffer != null) {
					if (staffer.getSuperiorId() != 0L) {
						Staffer departmentId1 = (Staffer) this.stafferDao.get(
								Staffer.class, staffer.getSuperiorId());
						if (departmentId1 != null) {
							form.setSuperiorName(departmentId1.getStafferName());
							form.getStaffer().setSuperiorId(
									departmentId1.getId());
						}
					}

					if (staffer.getBirthday() != null) {
						form.setBirthdayStr(sf.format(staffer.getBirthday()));
					}

					if (staffer.getInDate() != null) {
						form.setInDateStr(sf.format(staffer.getInDate()));
					}

					if (staffer.getLeaveDate() != null) {
						form.setLeaveDateStr(sf.format(staffer.getLeaveDate()));
					}
				}
			} else {
				staffer = new Staffer();
				staffer.setInDate(new Date());
				staffer.setCertName("身份证");
				form.setInDateStr(sf.format(staffer.getInDate()));
				form.setStaffer(staffer);
				String departmentId = request.getParameter("department_id");
				if (departmentId != null && !departmentId.equals("")) {
					form.getStaffer().setDepartmentId(
							Long.parseLong(departmentId));
				}
			}

			return form;
		}
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		String id = request.getParameter("id");
		String alertMsg = "";
		StafferBean form = (StafferBean) command;
		Staffer obj = form.getStaffer();
		obj.setNameSpell(PageHelper.getPinYinHeadChar(obj.getStafferName())
				.toUpperCase());
		String oldSuperiorIdStr = "";
		if (obj.getSuperiorIdStr() != null
				&& !obj.getSuperiorIdStr().equals("")) {
			oldSuperiorIdStr = obj.getSuperiorIdStr();
		}

		String superiorIdStr = "";
		if (obj.getSuperiorId() != 0L) {
			Staffer oldFile = (Staffer) this.stafferDao.get(Staffer.class,
					obj.getSuperiorId());
			if (oldFile != null) {
				if (oldFile.getSuperiorIdStr() != null
						&& !oldFile.getSuperiorIdStr().equals("")) {
					superiorIdStr = oldFile.getSuperiorIdStr()
							+ oldFile.getId() + ";";
				} else {
					superiorIdStr = ";" + oldFile.getId() + ";";
				}
			}
		}

		obj.setSuperiorIdStr(superiorIdStr);
		String arg22 = obj.getAccessory();
		String uploadPath = EncCommonHelper.getUploadVirtualPath(this
				.getServletContext());
		String realPath = this.getServletContext().getRealPath(uploadPath);
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile multipartFile = multipartRequest
				.getFile("accessory_file");
		String mv;
		String departmentId;
		String continueFlag;
		if (multipartFile != null && !multipartFile.isEmpty()) {
			mv = multipartFile.getOriginalFilename();
			departmentId = EncCommonHelper.getExtendName(mv);
			continueFlag = ub.getUser().getId() + "_"
					+ System.currentTimeMillis() + "." + departmentId;
			FileCopyUtils.copy(multipartFile.getBytes(), new File(realPath
					+ System.getProperty("file.separator") + continueFlag));
			obj.setAccessory(uploadPath + continueFlag);
			if (arg22 != null && !arg22.equals("")) {
				try {
					File newStr = new File(realPath
							+ System.getProperty("file.separator") + arg22);
					if (newStr.exists()) {
						newStr.delete();
					}
				} catch (Exception arg21) {
					;
				}
			}
		}

		multipartFile = multipartRequest.getFile("attach_file");
		if (multipartFile != null && !multipartFile.isEmpty()) {
			mv = multipartFile.getOriginalFilename();
			departmentId = EncCommonHelper.getExtendName(mv);
			continueFlag = ub.getUser().getId() + "img_"
					+ System.currentTimeMillis() + "." + departmentId;
			FileCopyUtils.copy(multipartFile.getBytes(), new File(realPath
					+ System.getProperty("file.separator") + continueFlag));
			obj.setImage(uploadPath + continueFlag);
		}

		if (id != null && !id.equals("")) {
			this.stafferDao.update(obj);
			if (!oldSuperiorIdStr.equals(superiorIdStr)) {
				List arg24 = this.stafferDao.getAllChildren(obj.getId(), false);
				continueFlag = "";
				if (oldSuperiorIdStr.equals("")) {
					continueFlag = ";" + obj.getId() + ";";
				} else {
					continueFlag = oldSuperiorIdStr + obj.getId() + ";";
				}

				String arg27 = "";
				if (superiorIdStr.equals("")) {
					arg27 = ";" + obj.getId() + ";";
				} else {
					arg27 = superiorIdStr + obj.getId() + ";";
				}

				for (int i = 0; i < arg24.size(); ++i) {
					Staffer arg26 = (Staffer) arg24.get(i);
					arg26.setSuperiorIdStr(arg26.getSuperiorIdStr().replaceAll(
							continueFlag, arg27));
					this.stafferDao.update(arg26);
				}
			}

			if (obj.getEnabled().equals("N") && obj.getId() != -1L) {
				this.userDao.unenabledStafferUser(obj.getId());
			}

			alertMsg = "修改成功！";
		} else {
			obj.setId(this.idGenerator.getId(Staffer.class.getName()));
			if (!form.getCodeRule().getGenerateRule().equals("MANUAL")) {
				obj.setStafferCode(PageHelper.generateCode(
						this.serialNumberDao, form.getCodeRule(), "", 0L));

				for (Staffer arg23 = this.stafferDao.get(obj.getStafferCode()); arg23 != null; arg23 = this.stafferDao
						.get(obj.getStafferCode())) {
					obj.setStafferCode(PageHelper.generateCode(
							this.serialNumberDao, form.getCodeRule(), "", 0L));
				}
			}

			this.stafferDao.save(obj);
			alertMsg = "增加成功！";
		}

		departmentId = request.getParameter("department_id");
		if (departmentId == null) {
			departmentId = "";
		}

		continueFlag = request.getParameter("continue_flag");
		ModelAndView arg25;
		if (continueFlag != null && continueFlag.equals("1")) {
			arg25 = new ModelAndView(
					"redirect:/base/StafferNew.ihtm?department_id="
							+ departmentId);
		} else {
			arg25 = new ModelAndView(this.getSuccessView());
			arg25.addObject("returnValue", String.valueOf(obj.getId()));
		}

		EncCommonHelper.setAlertMessage(request, alertMsg);
		return arg25;
	}

	public DepartmentDao getDepartmentDao() {
		return this.departmentDao;
	}

	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public StafferDao getStafferDao() {
		return this.stafferDao;
	}

	public void setStafferDao(StafferDao stafferDao) {
		this.stafferDao = stafferDao;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public SerialNumberDao getSerialNumberDao() {
		return this.serialNumberDao;
	}

	public void setSerialNumberDao(SerialNumberDao serialNumberDao) {
		this.serialNumberDao = serialNumberDao;
	}

	public UserDao getUserDao() {
		return this.userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
}